package com.itheima.youdianda.controller;

import com.itheima.youdianda.common.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/common")
public class CommonController {
    @Value("${youdianda.path}")
    private String basePath;
    /**
     * 上传接口
     *
     * @param
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        String filename = file.getOriginalFilename();
        String substring = filename.substring(filename.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString();
        String newFileName=uuid +substring;
        File dir = new File(basePath);
        if (!dir.exists()){
            dir.mkdirs();
        }
        try {
            file.transferTo(new File(basePath+newFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.ok(newFileName);
    }

}
